package cn.wolfcode.wolfcodemall.service.impl;

import cn.wolfcode.wolfcodemall.domain.Admin;
import cn.wolfcode.wolfcodemall.mapper.AdminMapper;
import cn.wolfcode.wolfcodemall.service.AdminService;
import cn.wolfcode.wolfcodemall.util.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    public Admin login(String username, String password, String verifycode, HttpServletRequest request) {
        //登录逻辑
        //先判断验证码是否正确
        boolean verification = VerifyCodeUtil.verification(request, verifycode, true);
        //如果验证码不正确，
        if(!verification){
            //登录失败，抛出一个异常
            throw new RuntimeException("验证码输入错误");
        }
        //如果验证码正确，拿着用户名和密码从数据库查询
        Admin login = adminMapper.login(username, password);
        //如果查询没有查询到数据，
        if(login == null){
            //登录失败，抛出一个异常
            throw new RuntimeException("账号或密码输入错误");
        }
        //如果查询到了数据，登录成功
        return login;
    }

    @Override
    public void updateAdmin(Map<String, Object> map) {
        adminMapper.updateAdmin(map);
    }


}
